package com.luugiathuy.apps.remotebluetooth;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.ToggleButton;

public class EditMacroActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit_macro);
        
        Spinner spnLettersNumbers = (Spinner) findViewById(R.id.spnLettersAndNumbers);
        
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.AlphabetNumeric, R.layout.my_spinner_textview);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //adapter.setDropDownViewResource(R.layout.my_spinner_textview);
        spnLettersNumbers.setAdapter(adapter);
        spnLettersNumbers.setOnItemSelectedListener(new MyOnItemSelectedListener());
        
        
        final Macro macro1 = new Macro();
        
        ListView scrl = (ListView) findViewById(R.id.scrlActions);
        final ArrayAdapter adpt = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, macro1.actionList);
        scrl.setAdapter(adpt);

        
        
        final Button btnAddMacro = (Button) findViewById(R.id.btnAddMacro);
        
        btnAddMacro.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Action act = createAction();
                macro1.addAction(act);
                macro1.reloadList();
                adpt.notifyDataSetChanged();
                ToggleButton btnCtrl = (ToggleButton) findViewById(R.id.tglCtrl);
        		ToggleButton btnShift = (ToggleButton) findViewById(R.id.tglShift);
        		ToggleButton btnAlt = (ToggleButton) findViewById(R.id.tglAlt);
        		Spinner spnLettersNumbers = (Spinner) findViewById(R.id.spnLettersAndNumbers);
        		btnCtrl.setChecked(false);
        		btnShift.setChecked(false);
        		btnAlt.setChecked(false);
        		spnLettersNumbers.setSelection(0);
            }
        });
        
        final Button btnClearMacro = (Button) findViewById(R.id.btnClearMacro);
        
        btnClearMacro.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	macro1.emptyActionList();
                macro1.reloadList();
                adpt.notifyDataSetChanged();
            }
        });

	}
	
	public Action createAction()
	{
		ToggleButton btnCtrl = (ToggleButton) findViewById(R.id.tglCtrl);
		ToggleButton btnShift = (ToggleButton) findViewById(R.id.tglShift);
		ToggleButton btnAlt = (ToggleButton) findViewById(R.id.tglAlt);
		Spinner spnLettersNumbers = (Spinner) findViewById(R.id.spnLettersAndNumbers); 
		String keyPressed = spnLettersNumbers.getSelectedItem().toString();
		Action act = new Action();
		if(btnCtrl.isChecked())
			act.addAction("Ctrl");
		if(btnShift.isChecked())
			act.addAction("Shift");
		if(btnAlt.isChecked())
			act.addAction("Alt");
		act.addAction(keyPressed);
		return act;
		
	}
	
	
}
